package com.psa.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.psa.element.MainMenuItem;
import com.psa.system.R;
import com.wxw.BaseLibrary.drawable.PanelStyle;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author david
 * @date 2014-08-06
 */
public class ActionItemAdapter extends BaseAdapter {
    private Context _context=null;
    private LayoutInflater _inflater = null;
    private List<MainMenuItem> items = new ArrayList<MainMenuItem>();

    public int getSelectedIndex() {
        return selectedIndex;
    }

    public void setSelectedIndex(int selectedIndex) {
        this.selectedIndex = selectedIndex;
    }

    private int selectedIndex = -1;

    public ActionItemAdapter(Context context, List<MainMenuItem> data){
        _context = context;
        _inflater = LayoutInflater.from(_context);
        items = data;
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items == null || items.size() < position?null:items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        HelperClass classHelper = null;
        MainMenuItem item = items.get(position);
        if (convertView == null){
            classHelper = new HelperClass();
            convertView = _inflater.inflate(R.layout.gridbuttonitem, null);
            classHelper.textView = (TextView)convertView.findViewById(R.id.ItemText);
            convertView.setTag(classHelper);
        } else {
            classHelper = (HelperClass)convertView.getTag();
        }
        if (selectedIndex == position){
            GradientDrawable gradientDrawable = PanelStyle.createPanelStyle(GradientDrawable.Orientation.TOP_BOTTOM, new int[]{Color.parseColor("#94CBFF")}, 2, Color.parseColor("#5079B2"), 8);
            convertView.setBackground(gradientDrawable);
        } else {
            convertView.setBackgroundColor(Color.TRANSPARENT);
        }
        classHelper.textView.setText(item.Title);
        return convertView;
    }

    class HelperClass{
        public TextView textView;
    }
}
